package com.aaa.service.impl;

import com.aaa.entity.Leadingusers;
import com.aaa.mapper.LeadingUsersMapper;
import com.aaa.service.LeadingUsersService;
import com.aaa.util.DefaultMsg;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.javafx.binding.StringFormatter;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

@Service
@Transactional
public class LeadingUsersServiceImpl extends ServiceImpl<LeadingUsersMapper, Leadingusers> implements LeadingUsersService {
    @Resource
    private RedisTemplate<String, String> redisTemplate;
    @Resource
    private LeadingUsersMapper leadingUsersMapper;

    @Override
    public DefaultMsg addLeadingUsers(Leadingusers leadingusers) {
        leadingusers.setLhead("头像");
        leadingusers.setLname("默认昵称");
        leadingusers.setLsex("男");
//        默认地址
        leadingusers.setAddress("默认地址");
//        默认创建时间
        Date date = new Date();
//        格式化时间
        String format = String.format("%tF %<tT", date);
        leadingusers.setCreateDate(format);
//        默认账号状态
        leadingusers.setState("启用");
//       加密密码
        String password = leadingusers.getPassword();
        PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encode = passwordEncoder.encode(password);
        leadingusers.setPassword(encode);
        DefaultMsg defaultMsg = new DefaultMsg();
//       获取用户真实验证码
        String code = redisTemplate.opsForValue().get("eCode");
//       if code为null 验证码过期了
        if (code == null) {
            defaultMsg.setCode(500);
            defaultMsg.setSuccess(0);
            defaultMsg.setMsg("验证码过期了，请重新获取");
        } else {
            if (code.equals(leadingusers.getCode())) {
                int insert = baseMapper.insert(leadingusers);
                if (insert > 0) {
                    defaultMsg.setCode(200);
                    defaultMsg.setSuccess(1);
                    defaultMsg.setMsg("注册成功");
                }
            } else {
                defaultMsg.setCode(500);
                defaultMsg.setSuccess(0);
                defaultMsg.setMsg("验证码错误");
            }
        }
        return defaultMsg;
    }

    @Override
    public DefaultMsg checkUsername(String username) {
        Leadingusers leadingusers = leadingUsersMapper.checkUsername(username);
//        判断用户名是否存在
        DefaultMsg defaultMsg = new DefaultMsg();
        if (leadingusers != null) {
            defaultMsg.setSuccess(0);
            defaultMsg.setMsg("用户名已存在");
        } else {
            defaultMsg.setSuccess(1);
            defaultMsg.setMsg("用户名可用");
        }
        return defaultMsg;
    }

    @Override
    public DefaultMsg updateLeadingusers(Leadingusers leadingusers) {
        DefaultMsg defaultMsg = new DefaultMsg();
        int i = leadingUsersMapper.updateLeadingusers(leadingusers);
        if (i > 0) {
            defaultMsg.setSuccess(1);
            defaultMsg.setMsg("修改成功");
        } else {
            defaultMsg.setSuccess(0);
            defaultMsg.setMsg("修改失败");
        }
        return defaultMsg;
    }

    @Override
    public DefaultMsg updatePassword(String oldPassword, String password, Integer lid) {
        DefaultMsg defaultMsg = new DefaultMsg();
//        获取旧密码
        Leadingusers leadingusers = leadingUsersMapper.selectPassword(lid);
        String oldPassword1 = leadingusers.getPassword();
        PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
//      BCryptPasswordEncoder判断加密的密码
        boolean matches = passwordEncoder.matches(oldPassword, oldPassword1);
        if (!matches) {
            defaultMsg.setSuccess(0);
            defaultMsg.setMsg("旧密码错误");
            return defaultMsg;
        } else {
            Leadingusers leadingusers1 = new Leadingusers();
            leadingusers1.setLid(lid);
            String encode = passwordEncoder.encode(password);
            leadingusers1.setPassword(encode);
            int i = leadingUsersMapper.updatePassword(leadingusers1);
            if (i > 0) {
                defaultMsg.setSuccess(1);
                defaultMsg.setMsg("修改成功");
            } else {
                defaultMsg.setSuccess(0);
                defaultMsg.setMsg("修改失败");
            }
            return defaultMsg;
        }
    }
}
